home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / statfs.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  3KB  |  127 lines

  1. /*
  2.  * statfs() emulation for MiNT/TOS
  3.  *
  4.  * Written by Adrian Ashley (adrian@secret.uucp)
  5.  * and placed in the public domain.
  6.  */
  7.  
  8. #include <errno.h>
  9. #include <stat.h>
  10. #include <osbind.h>
  11. #include <mintbind.h>
  12. #include <unistd.h> /* for chdir, getcwd */
  13. #include <limits.h> /* for PATH_MAX */
  14. #include <support.h>
  15. #ifdef __TURBOC__
  16. #include <sys\statfs.h>
  17. #else
  18. #include <sys/statfs.h>
  19. #endif
  20.  
  21. extern int __mint;
  22.  
  23. int statfs(path, buf)
  24.   const char *path;
  25.   struct statfs *buf;
  26. {
  27.   int r;
  28.   _DISKINFO free;
  29.   struct stat statbuf;
  30.   struct {
  31.     long ninodes, nzones;
  32.     long finodes, fzones;
  33.     short version;
  34.     short increment;
  35.     long res1, res2, res3, res4;
  36.   } mfsinfo;
  37.   char _path[PATH_MAX];
  38.  
  39.   if (!buf || !path)
  40.   {
  41.     errno = EFAULT;
  42.     return -1;
  43.   }
  44.  
  45.   r = stat(path, &statbuf);
  46.  
  47.   if (r == -1)
  48.     return -1;
  49.  
  50. /* This bit courtesy of S N Henson:
  51.  * We can do better than Dfree with minix filesystems
  52.  * they do have inodes and the Dcntl function MFS_INFO
  53.  * (0x104) tells us how many exist and how many are free.
  54.  * Also f_type is 1 for V1 filesystems and 2 for V2 (it
  55.  * is zero for TOS).
  56.  */
  57.   _unx2dos (path, _path);
  58.   if(Dcntl(0x104,_path, (long) &mfsinfo)==0)
  59.   {
  60.     buf->f_type = 1+mfsinfo.version;
  61.     buf->f_bsize = 1024;
  62.     buf->f_blocks = mfsinfo.nzones;
  63.     buf->f_bfree = buf->f_bavail = mfsinfo.fzones;
  64.     buf->f_files = mfsinfo.ninodes;
  65.     buf->f_ffree = mfsinfo.finodes;
  66.     buf->f_fsid.val[0] = buf->f_fsid.val[1] = -1L;
  67.   }
  68.   else {
  69.     if ((__mint >= 99) && (statbuf.st_dev >= 32))
  70.     {
  71.       /* Hack by HPP 02/06/1993: since MiNT 0.99 only returns     */
  72.       /* valid dfree info for pseudo-drives if they are the       */
  73.       /* current directory, change directories for the occasion.  */
  74.       char oldpath[PATH_MAX];
  75.     
  76.       if (getcwd(oldpath, PATH_MAX) != NULL)
  77.       {
  78.         chdir(path);
  79.         Dfree(&free, statbuf.st_dev + 1);
  80.         chdir(oldpath);
  81.       }
  82.       else
  83.         Dfree(&free, statbuf.st_dev + 1);
  84.     }
  85.     else
  86.       Dfree(&free, statbuf.st_dev + 1);
  87.  
  88.     buf->f_type = 0;
  89.     buf->f_bsize = free.b_secsiz * free.b_clsiz;
  90.     buf->f_blocks = free.b_total;
  91.     buf->f_bfree = buf->f_bavail = free.b_free;
  92.     buf->f_files = buf->f_ffree = buf->f_fsid.val[0] = buf->f_fsid.val[1] = -1L;
  93.   }
  94.   return 0;
  95. }
  96.  
  97. #ifdef TEST
  98.  
  99. #include <stdio.h>
  100.  
  101. main(int argc, char **argv)
  102. {
  103.   int i = 0, r;
  104.   register char *p;
  105.   struct statfs stbuf;
  106.  
  107.   while (--argc)
  108.   {
  109.     p = argv[++i];
  110.  
  111.     r = statfs(p, &stbuf);
  112.     if (r == -1)
  113.       perror(p);
  114.     else
  115.     {
  116.       printf("statfs(`%s'): %ld free bytes\n", p,
  117.     (long)(stbuf.f_bfree * stbuf.f_bsize));
  118.       printf("Fs type %ld\n",stbuf.f_type);
  119.       printf("%ld zones %ld free zones\n",stbuf.f_blocks,stbuf.f_bfree);
  120.       printf("%ld nodes %ld free nodes\n",stbuf.f_files,stbuf.f_ffree);
  121.     }
  122.   }
  123.   return 0;
  124. }
  125.  
  126. #endif
  127.